﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Windows.Forms;
using SmartMotion.Core;
using Sunny.UI;
using WeifenLuo.WinFormsUI.Docking;

namespace SmartMotion.AllPages.DockPanels
{
    public partial class FrmVar : DockContent
    {
        private ConcurrentDictionary<string, VarInfo> _vars;
        public FrmVar()
        {
            InitializeComponent();
            HideOnClose = true;
        }

        private void FrmVar_Load(object sender, EventArgs e)
        {

            if (GlobalVars.Ins.Vars.ContainsKey(GlobalVars.FlowVars))
            {
                _vars = GlobalVars.Ins.Vars[GlobalVars.FlowVars];
            }

            SetGrid();

            RefreshData();
        }

        private void RefreshData()
        {
            var bs = new BindingSource();
            bs.DataSource = _vars.Values;
            grid.DataSource = bs;
        }

        private void SetGrid()
        {
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            grid.RowHeadersVisible = false;
            grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.AllowUserToAddRows = false;
            grid.MultiSelect = false;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void 增加变量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var varInfo = new VarInfo();
            var typesList = new List<string>
            {
                typeof(bool).FullName,
                typeof(short).FullName,
                typeof(ushort).FullName,
                typeof(int).FullName,
                typeof(uint).FullName,
                typeof(long).FullName,
                typeof(ulong).FullName,
                typeof(string).FullName,
                typeof(double).FullName,
                typeof(float).FullName,
            };
            int selIndex = 0;
            
            if (this.ShowSelectDialog(ref selIndex,typesList))
            {
                varInfo.Type = typesList[selIndex];
            }
            retry:
            string varName=String.Empty;
            if (UIInputDialog.InputStringDialog(ref  varName,true,"请输入变量名称"))
            {
                if (_vars.ContainsKey(varName))
                {
                    UIMessageBox.ShowError($"变量[{varName}]已存在，请换一个");
                    goto  retry;
                }

                varInfo.Name = varName;

            }
            else
            {
                return;
            }
            string group =String.Empty;
            if (UIInputDialog.InputStringDialog(ref group,true,"请输入分组名称"))
            {
                varInfo.Group = group;
            }

            string value = string.Empty;
            if (UIInputDialog.InputStringDialog(ref value,true,"请输入变量初始值"))
            {
                dynamic dyValue = null;
                if (varInfo.Type == typeof(bool).FullName)
                {
                    dyValue = Convert.ToBoolean(value);
                }

                if (varInfo.Type == typeof(Int16).FullName)
                {
                    dyValue = Convert.ToInt16(value);
                }
                if (varInfo.Type == typeof(UInt16).FullName)
                {
                    dyValue = Convert.ToUInt16(value);
                }
                if (varInfo.Type==typeof(int).FullName)
                {
                    dyValue = Convert.ToInt32(value);
                }
                if (varInfo.Type == typeof(uint).FullName)
                {
                    dyValue = Convert.ToUInt32(value);
                }
                if (varInfo.Type == typeof(long).FullName)
                {
                    dyValue = Convert.ToInt64(value);
                }
                if (varInfo.Type == typeof(ulong).FullName)
                {
                    dyValue = Convert.ToUInt64(value);
                }
                if (varInfo.Type == typeof(double).FullName)
                {
                    dyValue = Convert.ToDouble(value);
                }
                if (varInfo.Type == typeof(Single).FullName)
                {
                    dyValue = Convert.ToSingle(value);
                }

                if (varInfo.Type == typeof(string).FullName)
                {
                    dyValue = value;
                }
                varInfo.Value = dyValue;
                varInfo.InitValue = dyValue;
                _vars.TryAdd(varInfo.Name, varInfo);
                RefreshData();

            }

        }

        private void 删除变量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (grid.CurrentRow?.DataBoundItem is VarInfo par)
            {
                _vars.TryRemove(par.Name, out var varInfo);
                RefreshData();
            }
        }

        private void 增加对象ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var varInfo = new VarInfo();
            var typeList = new List<string>()
            {
                typeof(object).FullName,
            };
            int selIndex = 0;
            if (this.ShowSelectDialog(ref selIndex,typeList))
            {
                varInfo.Type = typeList[selIndex];
            }
            retry:
            string objName=string.Empty;
            if (UIInputDialog.InputStringDialog(ref objName,true,"请输入对象名称"))
            {
                if (_vars.ContainsKey(objName))
                {
                    UIMessageBox.ShowError($"对象[{objName}]已存在,请重新输入一个对象名称");
                    goto retry;
                }
            }

            varInfo.Name = objName;
            string group =string.Empty;
            if (UIInputDialog.InputStringDialog(ref group,true,"请输入分组名称"))
            {
                varInfo.Group = group;
            }

            varInfo.InitValue = null;
            varInfo.Value = null;

            _vars.TryAdd(varInfo.Name, varInfo);
            RefreshData();
        }
    }
}
